package com.fanqwz.kotlinmvvm.di.module

import android.content.Context
import android.content.res.Resources
import com.fanqwz.kotlinmvvm.BuildConfig
import com.fanqwz.kotlinmvvm.MyApplication
import com.fanqwz.kotlinmvvm.common.util.log.MyLogger
import com.fanqwz.kotlinmvvm.common.util.log.getLogger
import com.fanqwz.kotlinmvvm.databinding.AppDataBindingComponent
import com.fanqwz.kotlinmvvm.databinding.MyDataBindingComponent
import com.fanqwz.kotlinmvvm.domain.executor.PostExecutionThread
import com.fanqwz.kotlinmvvm.domain.executor.ThreadExecutor
import com.fanqwz.kotlinmvvm.executor.JobExecutor
import com.fanqwz.kotlinmvvm.executor.UiThread
import com.squareup.leakcanary.LeakCanary
import com.squareup.leakcanary.RefWatcher
import dagger.Module
import dagger.Provides
import javax.inject.Singleton

/**
 * Created by fanqwz on 2017/10/25.
 */

/* 除这个主模块，其他所有子模块必须使用构造器注解或者使用@Binds注解（或者静态@Provides），否则编译有问题*/
@Module
class AppModule {
    @Provides
    @Singleton
    fun provideContext(app: MyApplication): Context = app

    @Provides
    @Singleton
    fun provideResource(app: MyApplication): Resources = app.resources

    @Provides
    @Singleton
    fun provideDataBindingComponent(): AppDataBindingComponent = MyDataBindingComponent()

    @Provides
    @Singleton
    fun provideRefWatcher(app: MyApplication): RefWatcher = if (BuildConfig.DEBUG) LeakCanary.install(app) else RefWatcher.DISABLED

    @Provides
    fun provideExecutor(jobExecutor: JobExecutor): ThreadExecutor = jobExecutor

    @Provides
    fun providePostExecutionThread(uiThread: UiThread): PostExecutionThread = uiThread

    @Provides
    fun provideLogger(app: MyApplication): MyLogger {
        return getLogger(app::class.java)
    }
}